package oauth

import (
	"errors"
	//"github.com/astaxie/beego"
	"wxsdk/miniapp/core"
)

type AdminAppTokenRspResult struct {
	*core.ErrorResponse
	*AdminAppTokenRsp
}
type AdminAppTokenRsp struct {
	AccessToken  string `json:"access_token"`
	ExpiresIn    int    `json:"expires_in"`
}

// 获取大黄蜂商城后台的自有token
func  GetAdminAppToken(appId, appSecret string)(string, int, error){
	serverUrl := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret
	client := core.GetPostJSONClient()
	var rsp AdminAppTokenRspResult
	tbdata, err := client.Excute("GET", serverUrl, nil, &rsp)
	if err != nil {
		//beego.Critical(err.Error())
		return "", 0, err
	}
	if rsp.ErrorResponse != nil {
		//beego.Critical("error:", string(tbdata))
		return "", 0, errors.New(string(tbdata)) 
	}
	return rsp.AdminAppTokenRsp.AccessToken,rsp.AdminAppTokenRsp.ExpiresIn,nil
}
